package com.sscaipiao.android.activity.Base;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.sscaipiao.android.R;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.MathUtil;
import com.sscaipiao.android.utils.ShakeListener;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.widget.BallBase;
import com.sscaipiao.android.widget.RedBall;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Airay.Rong on 14-1-2.
 */
public class RedBallGroupTenActivity extends NormalActivity implements BallBase.onClickCallback {
	private List<Integer> redBallIds = new ArrayList<Integer>();
	private final static int GROUP_CONTENT = 10;
	private int nRandomRedballNum;
	private int nMaxRedballGroupNum;
	private int arRedBallID[] = {
			R.id.red_ball_00, R.id.red_ball_01, R.id.red_ball_02, R.id.red_ball_03, R.id.red_ball_04,
			R.id.red_ball_05, R.id.red_ball_06, R.id.red_ball_07, R.id.red_ball_08, R.id.red_ball_09,

			R.id.red_ball_10, R.id.red_ball_11, R.id.red_ball_12, R.id.red_ball_13, R.id.red_ball_14,
			R.id.red_ball_15, R.id.red_ball_16, R.id.red_ball_17, R.id.red_ball_18, R.id.red_ball_19,

			R.id.red_ball_20, R.id.red_ball_21, R.id.red_ball_22, R.id.red_ball_23, R.id.red_ball_24,
			R.id.red_ball_25, R.id.red_ball_26, R.id.red_ball_27, R.id.red_ball_28, R.id.red_ball_29,

			R.id.red_ball_30, R.id.red_ball_31, R.id.red_ball_32, R.id.red_ball_33, R.id.red_ball_34,
			R.id.red_ball_35, R.id.red_ball_36, R.id.red_ball_37, R.id.red_ball_38, R.id.red_ball_39,

			R.id.red_ball_40, R.id.red_ball_41, R.id.red_ball_42, R.id.red_ball_43, R.id.red_ball_44,
			R.id.red_ball_45, R.id.red_ball_46, R.id.red_ball_47, R.id.red_ball_48, R.id.red_ball_49,

			R.id.red_ball_50, R.id.red_ball_51, R.id.red_ball_52, R.id.red_ball_53, R.id.red_ball_54,
			R.id.red_ball_55, R.id.red_ball_56, R.id.red_ball_57, R.id.red_ball_58, R.id.red_ball_59,

			R.id.red_ball_60, R.id.red_ball_61, R.id.red_ball_62, R.id.red_ball_63, R.id.red_ball_64,
			R.id.red_ball_65, R.id.red_ball_66, R.id.red_ball_67, R.id.red_ball_68, R.id.red_ball_69,
	};
	private int arGroupLabelID[] = {
			R.id.group_label_line0,R.id.group_label_line1,R.id.group_label_line2,
			R.id.group_label_line3,R.id.group_label_line4,R.id.group_label_line5,
			R.id.group_label_line6
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addShakeListener();
	}
	public void addRedBallGroup(int nMaxGroup) {
		if (nMaxGroup > arRedBallID.length/GROUP_CONTENT) {
			return;
		}
		nMaxRedballGroupNum = nMaxGroup;
		for (int i = 0; i < nMaxGroup * GROUP_CONTENT; i++) {
			redBallIds.add(arRedBallID[i]);
			((RedBall) findViewById(arRedBallID[i])).setCallback(this);
		}
		for (int i = nMaxGroup * GROUP_CONTENT; i < arRedBallID.length ; i++) {
			((RedBall) findViewById(arRedBallID[i])).setVisibility(View.GONE);
			TextView tv = (TextView)findViewById(arGroupLabelID[i/10]);
			tv.setVisibility(View.GONE);

		}
	}

	public void setGroupLabel(String ... labels) {
		for (int i = 0; i < labels.length; i++) {
			TextView tv = (TextView)findViewById(arGroupLabelID[i]);
			if (tv != null) {
				tv.setText(labels[i]);
			}
		}
	}

	public BallBase getRedBallByIndex(int index) {
		return  (BallBase)findViewById(redBallIds.get(index));
	}

	public void addShakeListener() {
		ShakeListener shakeListener = new ShakeListener(this);
		shakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() {
			public void onShake() {
				randomChooseBalls();
			}
		});
	}

	public void setRadomBallNum(int nRedNum, int nBlueNum) {
		nRandomRedballNum = nRedNum;
	}
	public void setRadomBallNum(int nRedNum) {
		nRandomRedballNum = nRedNum;
	}
	public void randomChooseBalls() {
		if (redBallIds.size() != 0) {
			randomChooseRedBalls();
		}
	}

	private void randomChooseBallInGroup(int groupIndex) {
		int random = (int) (Math.random()*10);
		int ballId = groupIndex*10+random;
		RedBall Ball = (RedBall) (findViewById(redBallIds.get(ballId )));
		Ball.setSelected(true);
	}
	public void randomChooseRedBalls() {
		if (nRandomRedballNum == 0) {
			return;
		}
		resetAllRedBalls();
		for (int i = 0; i < nMaxRedballGroupNum; i++) {
			randomChooseBallInGroup(i);
		}
		reCalculatorSum();
	}

	public void resetAllRedBalls() {
		for (int i = 0; i < redBallIds.size(); i++) {
			RedBall ball = (RedBall) findViewById(redBallIds.get(i));
			ball.reset();
		}
	}

	public void reCalculatorSum() {
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_hint);
		int notecount = notecount();
		setMoney(String.format(sAgeFormat1, notecount + "", (notecount * 2) + ""));
	}
	public boolean checkChoseBallRulls(List<Integer> balls) {
		if (balls.size() < nRandomRedballNum) {
			return false;
		}
		boolean b0Pass = false;
		boolean b1Pass = (nMaxRedballGroupNum != 2);
		boolean b2Pass = (nMaxRedballGroupNum != 3);
		boolean b3Pass = (nMaxRedballGroupNum != 4);
		boolean b4Pass = (nMaxRedballGroupNum != 5);
		boolean b5Pass = (nMaxRedballGroupNum != 6);
		boolean b6Pass = (nMaxRedballGroupNum != 7);
		for (int i = 0; i < balls.size(); i++) {
			int index = balls.get(i);
			if (index <= 10) {
				b0Pass = true;
			} else {
				if (index <= 20) {
					b1Pass = true;
				} else {
					if (index <= 30) {
						b2Pass = true;
					} else {
						if (index <= 40) {
							b3Pass = true;
						} else {
							if (index <= 50) {
								b4Pass = true;
							} else {
								if (index <= 60) {
									b5Pass = true;
								} else {
									if (index <= 70) {
										b6Pass = true;
									}
								}
							}
						}
					}
				}
			}
		}
		if (b0Pass == false || b1Pass == false || b2Pass == false || b3Pass == false
				|| b4Pass == false || b5Pass == false || b6Pass == false) {
			return false;
		}

		return true;
	}

	private int checkExtraChoseBallNumber(List<Integer> balls) {
		int nExtraNum =0;
		int nBase = balls.get(0);
		for (int i = 1; i < balls.size(); i++) {
			int nNew = balls.get(i);
			if (nNew / ((nBase/10+1) * 10) == nBase / ((nBase/10+1) * 10)) {
				nExtraNum++;
			}
			nBase = nNew;
		}
		return nExtraNum;
	}

	private int calcStakeNumber(List<Integer> balls) {
		int n0 = 0;int n1 = 0;int n2 = 0;int n3 = 0;int n4 = 0;int n5 = 0;int n6 = 0;
		for (int i = 0; i < balls.size(); i++) {
			int nNew = balls.get(i);
			if (nNew <= 10) {
				n0++;
			}else if (nNew <= 20) {
				n1 ++;
			}else if (nNew <= 30) {
				n2 ++ ;
			}else if (nNew <= 40) {
				n3 ++;
			}else if (nNew <= 50) {
				n4 ++ ;
			}else if (nNew <= 60) {
				n5 ++ ;
			}else if (nNew <= 70) {
				n6 ++ ;
			}
		}
		switch (nMaxRedballGroupNum) {
			case 3:
				return n0 * n1 * n2 ;
			case 4:
				return n0 * n1 * n2 * n3 ;
			case 5:
				return n0 * n1 * n2 * n3 * n4 ;
			case 6:
				return n0 * n1 * n2 * n3 * n4 * n5 ;
			case 7:
				return n0 * n1 * n2 * n3 * n4 * n5 * n6;
			default:
		}

		return 1;
	}
	public int notecount() {
		int redNum = queryChoseRedBallNum();
		if (redNum == 0) {
			return 0;
		}else if (redNum < nRandomRedballNum) {
			return 0;
		} else {
			List<Integer> redChooseBalls = queryChoseRedBallArray();
			if (checkChoseBallRulls(redChooseBalls) == false) {
				return 0;
			} else {
				return calcStakeNumber(redChooseBalls) ;
			}
		}
	}

	@Override
	public void onBallClick(View ball) {
//		int choosedNum =   queryChoseRedBallNum();
//		if (choosedNum > 20) {
//			Toast.makeText(this, "你选择的红球数已经超过20个，不能再选",Toast.LENGTH_SHORT).show();
//			BallBase curBall = (BallBase) ball;
//			curBall.reset();
//			return;
//		}
		reCalculatorSum();
	}
	private void resetAllBalls() {
		if (redBallIds.size() != 0) {
			resetAllRedBalls();
		}
	}
	public void onClick(View v) {
		super.onClick(v);
		if (R.id.btn_radom == v.getId()) {
			if (redBallIds.size() != 0) {
				randomChooseRedBalls();
			}
		} else if (R.id.im_undo == v.getId()) {
			resetAllBalls();
			reCalculatorSum();
		} else if (R.id.btn_confirm == v.getId()) {
			//doConfirm();
		}
	}
	public int queryChoseRedBallNum() {
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = getRedBallByIndex(i);
			if (ball.isSelect()) {
				nBallNum ++;
			}
		}
		return nBallNum;
	}
	public List<Integer> queryChoseRedBallArray() {
		List<Integer> ChooseBalls = new ArrayList<Integer>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = (BallBase) findViewById(redBallIds.get(i));
			if (ball.isSelect()) {
				ChooseBalls.add(i+1);
			}
		}
		return ChooseBalls;
	}
	private List<String> queryChoseRedBallArray_Name() {
		List<String> ChooseBalls = new ArrayList<String>();
		int nBallNum = 0;
		BallBase ball = null;
		for (int i = 0; i < redBallIds.size(); i++) {
			ball = (BallBase) findViewById(redBallIds.get(i));
			if (ball.isSelect()) {
			    ChooseBalls.add(ball.getBallText());
			}
		}
		return ChooseBalls;
	}
	private void setupLostValues() {
		int redBallNum = redBallIds.size();
		for (int i = 0; i < redBallIds.size(); i++) {
			getRedBallByIndex(i).setLabel("");
		}
		if (lostValues == null) {
			return;
		}
		for (int i = 0; i < lostValues.size(); i++) {
			String  text = lostValues.get(i);
			if (i < redBallNum) {
				getRedBallByIndex(i).setLabel(text);
			}
		}
	}
	@Deprecated
	public boolean getLottLostValues() {
		try {
			lostValues = CaipiaoServiceHelper.getLottLostValues_SingleRedBall(this, user, mLottCode);
			return true;

		} catch (Exception e) {

		}
		return false;
	}
	public void setupLottLostValues() {
		Thread thr1 = new Thread(mTask_getLottLostValues, "10");
		thr1.start();
	}

	Runnable mTask_getLottLostValues = new Runnable() {
		public void run() {
			try {
				lostValues = CaipiaoServiceHelper.getLottLostValues_NPart(RedBallGroupTenActivity.this,
						user, mLottCode,nRandomRedballNum);
				if (lostValues.size() != 0) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							setupLostValues();
						}
					});
				}
			} catch (Exception e) {

			}
			closeProgressDailog();
		}
	};

	public String formatChooseBallString() {
		List<String> redChooseBalls = queryChoseRedBallArray_Name();
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_1);
		String number = String.format(sAgeFormat1, notecount() + "");
		String redBalls = "";
		String singleOrMult = "";

		for (String num : redChooseBalls) {
			redBalls += (num + Constants.STRING_SPLITE);
		}

		if (notecount() == 1) {
			singleOrMult = getResources().getString(R.string.double_color_ball_add_single);
		} else {
			singleOrMult = getResources().getString(R.string.double_color_ball_add_multiple);
		}

		return redBalls.substring(0, redBalls.length() - 1) + Constants.STRING_SPLITE_COMPLEX +
				singleOrMult + Constants.STRING_SPLITE_COMPLEX + number;
	}

	public boolean checkAndStartPayActivity(Class payActivityName) {
		List<String> choosen = user.getChoosen();
		if (choosen == null) {
			choosen = new ArrayList<String>();
		}
		String chooseString =  formatChooseBallString();
		if (!choosen.contains(chooseString)) {
			choosen.add(chooseString);
			user.setChoosen(choosen);
			user.setHistory(history);

			Intent intent = new Intent(this, payActivityName);
//			Bundle b = new Bundle();
//			b.putSerializable(Constants.USER, user);
//			b.putSerializable("date", date);
//			b.putSerializable("leftTime", leftTime);
//			intent.putExtras(b);
			startActivity(intent);

		} else {
			Toast.makeText(this, "该组合已在购买列表中请重新选择", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}